import Taro from '@tarojs/taro'
import { concatUrl } from '@/utils'
import { getNewSessionKey, getAuthCode } from '@/utils/auth'


export function goToLogin() {
  Taro.showModal({
    title: '温馨提示',
    content: '请先登录',
    cancelText: '取消',
    cancelColor: '#999999',
    confirmText: '去登录',
    confirmColor: '#12b3ff',
  }).then(res => {
    if (res.confirm) {
      // Taro.setStorageSync('IS_SESSIONKEY', null)
      // Taro.setStorageSync('IS_OPENID', null)
      loginMethod()
    }
    Taro.setStorageSync('HAS_EXPIRE', '')
  })
}

//获取当前路径保存，跳转登录页
export function loginMethod() {
  const pages = Taro.getCurrentPages()
  const route = pages[pages.length - 1].route
  const query = pages[pages.length - 1].options
  console.log('pages', pages)
  console.log(concatUrl(route, query))
  console.log('route', route)
  console.log('query', query)
  if (process.env.TARO_ENV === 'weapp') {
    Taro.setStorageSync('BACK_ROUTE', concatUrl(route, query))
    getNewSessionKey().then(() => { // 跳转过去的必须登录login拿到sessionKey，确保在登录的时候，不会sessionKey过期
      Taro.navigateTo({
        url: '/pages/user/login/index'
      })
    })
  } else {
    getAuthCode()
    let link = pages[pages.length - 1].data.root.uid
    Taro.setStorageSync('BACK_ROUTE', link)
    Taro.navigateTo({
      url: '/pages/user/login/index'
    })
  }


}
